<?php

namespace backend\models;

use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Contract;

/**
 * ContractSearch represents the model behind the search form of `backend\models\Contract`.
 */
class ContractSearch extends Contract
{
    public $contractorInn;
    public $contractorKpp;

    public $totalPriceLess;
    public $totalPriceMore;

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'id_contractor', 'id_status', 'id_donor', 'id_project', 'id_liable_user', 'id_purchase_method', 'id_purpose', 'id_organization_branch', 'registry_number', 'is_yearly'], 'integer'],
            [['subject', 'date_sign', 'date_realize', 'comment', 'own_number', 'contractor_number', 'dateSignStart', 'dateSignEnd', 'dateRealizeStart', 'dateRealizeEnd', 'contractorInn', 'contractorKpp'], 'safe'],
            [['total_price', 'totalPriceLess', 'totalPriceMore'], 'number'],
        ];
    }

        /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        $attributeLabels = parent::attributeLabels();
        $attributeLabels['contractorInn'] = 'ИНН контрагента';
        $attributeLabels['contractorKpp'] = 'КПП контрагента';
        $attributeLabels['totalPriceLess'] = 'Сумма (до <)';
        $attributeLabels['totalPriceMore'] = 'Сумма (от >)';
        return $attributeLabels;
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Contract::find();
        $query->joinWith(['contractor']);
        
        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'defaultPageSize' => 50,
            ],
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ]
            ] 
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'contract.id' => $this->id,
            'id_contractor' => $this->id_contractor,
            'total_price' => $this->total_price,
            'date_sign' => $this->date_sign,
            'date_realize' => $this->date_realize,
            'id_status' => $this->id_status,
            'id_donor' => $this->id_donor,
            'id_project' => $this->id_project,
            'id_liable_user' => $this->id_liable_user,
            'id_purchase_method' => $this->id_purchase_method,
            'id_purpose' => $this->id_purpose,
            'id_organization_branch' => $this->id_organization_branch,
            'registry_number' => $this->registry_number,
            'is_yearly' => $this->is_yearly,
        ]);

        $query->andFilterWhere(['like', 'subject', $this->subject])
            ->andFilterWhere(['like', 'own_number', $this->own_number])
            ->andFilterWhere(['like', 'contractor.inn', $this->contractorInn . '%', false])
            ->andFilterWhere(['like', 'contractor.kpp', $this->contractorKpp . '%', false])
            ->andFilterWhere(['like', 'contractor_number', $this->contractor_number])
            ->andFilterWhere(['like', 'comment', $this->comment])
            ->andFilterWhere(['<=', 'date_sign', $this->dateSignEnd])
            ->andFilterWhere(['>=', 'date_sign', $this->dateSignStart])
            ->andFilterWhere(['<=', 'date_realize', $this->dateRealizeEnd])
            ->andFilterWhere(['>=', 'date_realize', $this->dateRealizeStart])
            ->andFilterWhere(['<=', 'total_price', $this->totalPriceLess])
            ->andFilterWhere(['>=', 'total_price', $this->totalPriceMore]);

        return $dataProvider;
    }
}
